package com.ctgu.chatapp.view.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ctgu.chatapp.databinding.ItemCardviewBinding
import com.ctgu.chatapp.showPojo.CardView

class CardViewAdapter(val list: MutableList<CardView>) :
    RecyclerView.Adapter<CardViewAdapter.ViewHolder>() {

    class ViewHolder(val binding: ItemCardviewBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = run {
        val binding =
            ItemCardviewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.cardView = list[position]
    }

    override fun getItemCount() = list.size

    fun addCardView(cardView: CardView) {
        list.add(cardView)
        notifyItemRangeInserted(itemCount, 1)
    }

    fun firstAddCardView(list: MutableList<CardView>) {
        list.addAll(list)
        notifyItemRangeInserted(itemCount, list.size)
    }
}